package com.xh.bussiness.system.controller;

import com.xh.bussiness.system.bean.dto.DictionaryRelBean;
import com.xh.bussiness.system.bean.request.DictionaryRelRequest;
import com.xh.bussiness.system.service.IDictionaryRelService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 数据字典表 前端控制器
 * </p>
 *
 * @author daiwei
 * @since 2018-10-10
 */
@Api(tags="数据字典关系")
@RestController
@RequestMapping("/system/dictionaryRel")
public class DictionaryRelController extends BaseController {

    @Autowired
    private IDictionaryRelService dictionaryRelService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @ApiOperation(value="数据字典关系分页查询", notes = "数据字典关系分页查询")
    @PostMapping("/queryPage")
    public XhPage queryPage(XhPage page, DictionaryRelRequest bean){
        page=dictionaryRelService.queryDictionaryRelPage(page,bean);
        return page;
    }

    /**
     * 数据查询
     * @param request 数据字典编码
     * @return
     */
    @ApiOperation(value="数据字典关系查询", notes = "数据字典关系查询-返回所有匹配数据")
    @PostMapping("/queryAll")
    public AjaxResult queryAll(DictionaryRelRequest request){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",dictionaryRelService.queryDictionaryRel(request));
    }

    /**
     * 新增和保存
     * @param dictionaryRelBean 实体对象
     * @return
     */
    @ApiOperation(value="数据字典关系保存", notes = "数据字典关系保存")
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated DictionaryRelBean dictionaryRelBean){
        if(dictionaryRelBean.getId()==null){
            dictionaryRelService.insertObj(dictionaryRelBean);
        }else{
            dictionaryRelService.updateObj(dictionaryRelBean);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",dictionaryRelBean);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @ApiOperation(value="数据字典关系删除", notes = "数据字典关系删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="数据字典id", required = true, dataType = "String"),
    })
    @PostMapping("/deleteById")
    @Transactional
    public AjaxResult deleteById(String id){
        dictionaryRelService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @ApiOperation(value="数据字典关系批量删除", notes = "数据字典关系批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ids", value="数据字典id集合", required = true, dataType = "String[]"),
    })
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        dictionaryRelService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
}

